var path = require('path');
var webpack = require('webpack');

var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var CopyWebpackPlugin = require('copy-webpack-plugin');
var SRC = path.join(__dirname, "./front/base/");
var OUTPUT_TARGET = path.join(__dirname, './target/bundle');

var config = {
    entry: {
        app: ['bootup'],
        login: ['login/login'],
        vendor: ["angular", 'bootstrap', "angular-ui-router", "oclazyload", "angular-tree-control", "angular-ui-bootstrap"]
    },//入口文件
    output: {//打包输出的文件
        path: OUTPUT_TARGET,
        chunkFilename: 'assets/js/[name].bundle.js',
        filename: "assets/js/[name].bundle.js"
    },
    module: {
        loaders: [
             {test: /\.js$/, exclude: /node_modules/, loader: 'ng-annotate!babel'},//排出node_modules下的文件，使用es6转义
             {test: /\.html/, loader: 'raw'},
             {test: /\.jsp/, loader: 'raw'},
             {test: /\.css$/, loader: "style-loader!css-loader"},
             {test: /\.less$/, loader: ExtractTextPlugin.extract("style", "css?sourceMap!less?sourceMap")},
             //{test: /\.scss$/, loader: ExtractTextPlugin.extract("style", "css?sourceMap!sass?sourceMap")},
             { test: /\.(png|jpg)$/, loader: 'url-loader?limit=8192'},
             {test: require.resolve('jquery'),loader: 'expose-loader?$!expose-loader?jQuery'},//将jQuery暴露成全局变量
             // **IMPORTANT** This is needed so that each bootstrap js file required by
             // bootstrap-webpack has access to the jQuery object
             {test: /\.woff($|\?)|\.woff2($|\?)|\.ttf($|\?)|\.eot($|\?)|\.svg($|\?)/, loader: 'url-loader'},
             { test: /bootstrap\/js\//, loader: 'imports?jQuery=jquery' }
        ]
    },
    resolve: {// 现在你require文件的时候可以直接使用require('file')，不用使用require('file.coffee')
        root: [SRC]
    },
    plugins: [
        //将js中的css/less/scss样式抽取出来作为一个单独的css文件,当然是entry中对应的js文件中的。
        new ExtractTextPlugin("assets/css/[name].css", {
            allChunks: true
        }),
        new HtmlWebpackPlugin({
            filename: 'index.jsp',
            template: './src/main/webapp/WEB-INF/jsp/index.jsp',
            //chunks这个参数告诉插件要引用entry里面的哪几个入口
            chunks: ['app', 'vendor'],
            hash: true
        }),
        new HtmlWebpackPlugin({
            filename: 'login.jsp',
            template: './src/main/webapp/WEB-INF/jsp/login.jsp',
            //chunks这个参数告诉插件要引用entry里面的哪几个入口
            chunks: ['login'],
            hash: true
        }),
        new webpack.optimize.CommonsChunkPlugin("vendor", "assets/js/vendor.bundle.js"),

        new CopyWebpackPlugin([
            {from: 'front/base/scrum.js', to: 'assets/js/scrum.js'},
            {from: 'front/base/assets/css/bootstrap.min.css', to: 'assets/css/bootstrap.min.css' },
            {from: 'front/base/assets/fonts', to: 'assets/fonts'},
            {from: "./front/base/imgs", to: 'assets/imgs'}

        ])

        /*压缩
        new webpack.optimize.UglifyJsPlugin({
         compress: {
         warnings: false
         }
         })*/
    ]
};

module.exports = config;